package ui.swing.list;

import java.awt.Color;
import java.awt.Container;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.BorderFactory;
import javax.swing.DefaultListModel;
import javax.swing.JApplet;
import javax.swing.JButton;
import javax.swing.JList;
import javax.swing.JTextArea;
import javax.swing.border.Border;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;

import ui.swing.Console;

public class List extends JApplet {

	private int count = 0;

	private String[] flavors = { "Chocolate", "Strawberry",
			"Vanilla Fudge Swirl", "Mint Chip", "Moncha Almond Fudge",
			"Rum Raisin", "Praline Cream", "Mud Pie" };

	private DefaultListModel lItems = new DefaultListModel() /*
															 * {
															 * 
															 * @Override public
															 * int getSize() {
															 * // TODO
															 * Auto-generated
															 * method stub
															 * return 10; } }
															 */;

	private JList lst = new JList(lItems);

	private JTextArea t = new JTextArea(flavors.length, 20);

	private JButton b = new JButton("Add Item");

	private ActionListener b1 = new ActionListener() {

		public void actionPerformed(ActionEvent e) {
			if (count < flavors.length) {
				lItems.add(0, flavors[count++]);
			} else {
				// Disable, since there are no more
				// flavors left to be added to the List
				b.setEnabled(false);
			}
		}
	};

	private ListSelectionListener ll = new ListSelectionListener() {
		public void valueChanged(ListSelectionEvent e) {
			if (e.getValueIsAdjusting())
				return;
			t.setText("");
			Object[] items = lst.getSelectedValues();
			for (int i = 0; i < items.length; i++)
				t.append(items[i] + "\n");
		}
	};

	public void init() {
		Container cp = getContentPane();
		t.setEditable(false);
		cp.setLayout(new FlowLayout());
		// Create Borders for components:
		Border brd = BorderFactory.createMatteBorder(1, 1, 2, 2, Color.BLACK);
		lst.setBorder(brd);
		t.setBorder(brd);

		// Add the first four items to the List
		for (int i = 0; i < 4; i++)
			lItems.addElement(flavors[count++]);

		// Add items to the Content Pane for Display
		cp.add(t);
		cp.add(lst);
		cp.add(b);

		// Register event listeners
		lst.addListSelectionListener(ll);
		b.addActionListener(b1);
	}

	public static void main(String[] args) {
		Console.run(new List(), 500, 375);
	}
}
